2 Problem: 574 - Sum it up (UVa Online Judge)
5 Author: Andrés Mejía-Posada
14 typedef vector
< vector
<int> > matrix
;
16 //for sorting the answer
17 bool compare(const vector
<int> &a
, const vector
<int> &b
) {
18 int n
= a
.size(), m
= b
.size();
19 for (int i
=0; i
<n
&& i
<m
; ++i
){
20 if (a
[i
] > b
[i
]) return true;
21 if (b
[i
] > a
[i
]) return false;
26 void show(const vector
<int> &v
){
28 for (int i
=1, n
=v
.size(); i
<n
; ++i
){
36 while (scanf("%d %d", &t
, &n
) == 2 && n
){
39 for (int i
=0; i
<n
; ++i
){
43 dp
[0][0].push_back(vector
<int>());
45 dp
[0][m
[0]].push_back(vector
<int>(1, m
[0]));
47 for (int i
=1; i
<n
; ++i
){
48 for (int j
=0; j
<=t
; ++j
){
49 dp
[i
][j
] = dp
[i
-1][j
];
51 for (int k
=0; k
<dp
[i
-1][j
-m
[i
]].size(); ++k
){
52 dp
[i
][j
].push_back(dp
[i
-1][j
-m
[i
]][k
]);
53 dp
[i
][j
].back().push_back(m
[i
]);
59 /*for (int i=0; i<n; ++i){
60 for (int j=0; j<=t; ++j){
61 printf("(%d, %d) %d elementos: \n", i, j, dp[i][j].size());
62 for (int k=0; k<dp[i][j].size(); ++k){
63 for (int m=0; m<dp[i][j][k].size(); ++m){
64 printf("%d ", dp[i][j][k][m]);
71 matrix
&answer
= dp
[n
-1][t
];
72 sort(answer
.begin(), answer
.end(), compare
);
74 //printf("La respuesta tiene %d elementos\n", dp[n-1][t].size());
75 printf("Sums of %d:\n", t
);
76 if (answer
.size() == 0){
80 for (int i
=1; i
<answer
.size(); ++i
){
81 if (answer
[i
] != answer
[i
-1]) show(answer
[i
]);